package com.platform.hjt.erp.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.ErrorCode;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.erp.dao.ErpStorageDAO;
import com.platform.hjt.erp.model.ErpStorage;
import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.hjt.tools.date.DateCreator;

@Service
public class ErpStorageService extends BaseService{

	@Resource
	private ErpStorageDAO erpStorageDAO;
	
	public ReturnBody saveErpStorage(ErpStorage erpStorage){
		if(StringUtils.isNotBlank(erpStorage.getErpStorageId())){
			ErpStorage oldErpStorage = erpStorageDAO.get(erpStorage.getErpStorageId());
			if(!oldErpStorage.getSysCompanyInfo().getSysCompanyInfoId().equals(getSessionSysCompanyInfo().getSysCompanyInfoId())){
				return new ReturnBody(false, "删除错误，请联系管理员。错误代码:" + ErrorCode.NoPower.getKey());
			}
			
			erpStorageDAO.updatePerproties(erpStorage);
			return  new ReturnBody(true, "更新成功");
		}else{
			
			erpStorage.setCreateTime(DateCreator.getCurrentTime());
			erpStorage.setIsDel(false);
			erpStorage.setSysCompanyInfo(getSessionSysCompanyInfo());
			erpStorageDAO.saveJ(erpStorage);
			return  new ReturnBody(true, "添加成功");
		}
	}
	
	public ReturnBody delErpStorage(ErpStorage erpStorage){
		erpStorage.setIsDel(true);
		erpStorageDAO.updatePerproties(erpStorage);
		return  new ReturnBody(true, "删除成功");
	}
	
	public Page<ErpStorage> getErpStorageList(Page<ErpStorage> page, List<PropertyFilter> propertyFilterList){
		return erpStorageDAO.findPage(page, propertyFilterList);
	}
	
	public List<ErpStorage> getErpStorageList(List<PropertyFilter> filters){
		return erpStorageDAO.find(filters);
	}
	
	public ErpStorage getErpStorage(List<PropertyFilter> filters){
		return erpStorageDAO.findUniqueBy(filters);
	}
	
	public ErpStorage getErpStorage(String propertyName, Object value){
		return erpStorageDAO.findUniqueBy(propertyName, value);
	}
	
}
